package com.huaxin.device.dto.enums.schemes;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 数据库元数据工具类：提供所有表和字段的只读视图
 */
public class SchemaMetadata {

    /**
     * 获取所有表的基本信息（表名、中文标签）
     */
    public static List<TableInfo> getAllTables() {
        return Arrays.stream(DatabaseSchema.values())
                .map(table -> new TableInfo(
                        table.getTableName(),
                        table.getLabel()
                ))
                .collect(Collectors.toList());
    }

    /**
     * 获取所有表及其字段的完整结构
     */
    public static List<TableWithFields> getAllTablesWithFields() {
        return Arrays.stream(DatabaseSchema.values())
                .map(table -> new TableWithFields(
                        table.getTableName(),
                        table.getLabel(),
                        Arrays.stream(table.getFields())
                                .map(field -> new FieldInfo(
                                        field.getFieldName(),
                                        field.getLabel(),
                                        field.getType(),
                                        field.getDescription()
                                ))
                                .collect(Collectors.toList())
                ))
                .collect(Collectors.toList());
    }

    /**
     * 根据表名获取其所有字段
     *
     * @param tableName 表名（如 device_readings_circuit_breaker）
     * @return 字段列表，表不存在则返回空列表
     */
    public static List<FieldInfo> getFieldsByTableName(String tableName) {
        return Arrays.stream(DatabaseSchema.values())
                .filter(table -> table.getTableName().equalsIgnoreCase(tableName))
                .findFirst()
                .map(table -> Arrays.stream(table.getFields())
                        .map(field -> new FieldInfo(
                                field.getFieldName(),
                                field.getLabel(),
                                field.getType(),
                                field.getDescription()
                        ))
                        .collect(Collectors.toList()))
                .orElse(List.of());
    }

    // ==================== 内部数据类 ====================

    /**
     * 表基本信息
     */
    public static class TableInfo {
        private final String tableName;
        private final String label;

        public TableInfo(String tableName, String label) {
            this.tableName = tableName;
            this.label = label;
        }

        public String getTableName() {
            return tableName;
        }

        public String getLabel() {
            return label;
        }
    }

    /**
     * 字段信息
     */
    public static class FieldInfo {
        private final String fieldName;
        private final String label;
        private final String type;
        private final String description;

        public FieldInfo(String fieldName, String label, String type, String description) {
            this.fieldName = fieldName;
            this.label = label;
            this.type = type;
            this.description = description;
        }

        public String getFieldName() {
            return fieldName;
        }

        public String getLabel() {
            return label;
        }

        public String getType() {
            return type;
        }

        public String getDescription() {
            return description;
        }
    }

    /**
     * 表 + 字段列表
     */
    public static class TableWithFields {
        private final String tableName;
        private final String label;
        private final List<FieldInfo> fields;

        public TableWithFields(String tableName, String label, List<FieldInfo> fields) {
            this.tableName = tableName;
            this.label = label;
            this.fields = fields;
        }

        public String getTableName() {
            return tableName;
        }

        public String getLabel() {
            return label;
        }

        public List<FieldInfo> getFields() {
            return fields;
        }
    }
}
